package SqlJavaBean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;

public class StudentR {
	private Connection con;
	
	public StudentR(){
		DBBean db = new DBBean();
		con = db.getConnection();
	}
	public boolean isDone(String user){
		String sql="select * from Subject,RecentExam where user='"+user+"' and Subject.examName=RecentExam.examName";
		String res = runSql(sql);
		if(res!="") return true;
		else return false;
	}
	public String getPartGrade(String partID){
		return "";
	}
	public String getExamGrade(String examID){
		return "";
	}
	public String getPart(){
		String sql = "select * from Part";
		return runSql(sql);
	}
	public String getPartData(String partName,String user){
		String sqlx = "select grade,Practise.practiseID,data,anserData from Practise,PartDataBank ";
		sqlx = sqlx+ "where Practise.practiseID=PartDataBank.practiseID and Practise.partName='"+partName+"'";
		sqlx = sqlx+" and user='"+user+"'";
		String res = runSql(sqlx);
		if(res!="") return res;
		String sql = "select * from PartDataBank where partName='"+partName+"'";
		return runSql(sql);
	}
	
	public String getExamTime(){
		String sql = "select whenLong from Exam,RecentExam where Exam.examName=RecentExam.examName";
		return runSql(sql);
	}
	
	public String getExamData(){ //从题库中随机抽取指定数目的题目发于考生
		String res="";
		String[] swap;
		ResultSet rs;
		int c=0;
		int counts = 0;
		try{
			Statement s = con.createStatement();
			rs = s.executeQuery("select subjectCounts from Exam,RecentExam where Exam.examName=RecentExam.examName"); //获取此次考试的相关信息：每个考生多少道题
			if(rs.next()) counts = rs.getInt(1);
			rs = s.executeQuery("select count(*) from ExamDataBank,RecentExam where ExamDataBank.examName=RecentExam.examName");
			if(rs.next()) c = rs.getInt(1);
			if(counts>c) counts = c; 
			
			String sql = "select subjectID,data";
			sql = sql+" from ExamDataBank,RecentExam where ExamDataBank.examName=RecentExam.examName";
			rs = s.executeQuery(sql);
			while(rs.next()){
				res = res+rs.getString(1)+"\u0002"+rs.getString(2)+"\u0001";
			}
			swap = res.split("\u0001");
			res = "";
			 //抽题
			int[] ran = random(c,counts);
			for(int i=0;i<counts;i++) res = res+swap[ran[i]]+"\u0001";
		}
		catch(Exception e){
			res = res+"ddd#"+e.getMessage();
		}
		return res;
	}
	
	private int[] random(int total,int counts){
		int[] res = new int[counts];
		int x = total/counts;
		Random r = new Random();
		int swap = 0;
		boolean flag = true;
		for(int i=0;i<counts;i++){
			swap = r.nextInt(total);
			for(int j=0;j<i;j++)
				if(swap==res[j]){
					flag = false;
					break;
				}
			if(flag) res[i] = swap;
			else i--;
			flag = true;
		}
		return res;
	}
	
//**********************************************************************************
	private String runSql(String sql){
		String res = "";
		try{
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			return getResult(rs);
		}
		catch(Exception e){
			res = res + e.getMessage();
		}
		return "";
	}
	private String getResult(ResultSet rs){
		String res = "";
		try{
			int count = rs.getMetaData().getColumnCount();
			while(rs.next()){
				for(int i=1;i<=count;i++) res = res+rs.getString(i)+"\u0002";
				res = res+"\u0001";
			}
			return res;
		}
		catch(Exception e){
			res = res+e.getMessage();
		}
		
		return "";
	}
	
	public static void main(String[] s){
		StudentR stu = new StudentR();
		String sql = "select * from PartDataBank where partName='第一章练习'";
		System.out.print(stu.getPartData("xxxTestA","testa"));
	}
}
